package com.demo.hotel_book.controller;

import com.demo.hotel_book.entity.HotelExample;
import com.demo.hotel_book.entity.User;
import com.demo.hotel_book.mapper.HotelMapper;
import com.demo.hotel_book.mapper.HotelOrderMapper;
import com.demo.hotel_book.entity.Hotel;
import com.demo.hotel_book.service.HotelOrderService;
import com.demo.hotel_book.entity.HotelOrder;
import com.demo.hotel_book.util.SessionUtil;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

@Controller
public class HotelOrderController {
    Integer hotelId;
    Integer roomPrice;
    User user;
    @Autowired
    HotelOrderMapper orderMapper;
    @Autowired
    private HotelOrderService orderService;

    @Autowired
    HotelMapper hotelMapper;
    @GetMapping("/rooms")
//    @ResponseBody
    public String findById(@RequestParam("id") Integer id, Model model, HttpSession session) {
        if (session.getAttribute("user")!=null||session.getAttribute("timeIn")!=null)
        {
            user = (User) session.getAttribute("user");
            String timeIn = (String) session.getAttribute("timeIn");
            String timeOut = (String) session.getAttribute("timeOut");
            model.addAttribute("timeIn", timeIn);
            model.addAttribute("timeOut", timeOut);
        }

        model.addAttribute("hotel", orderService.findById(id));
        hotelId = id;
        String param = (String) SessionUtil.getSession().getAttribute("param");
        model.addAttribute("param1", param);

        return "rooms";
    }

    @GetMapping("/details")
//    @ResponseBody
    public String orderFindById(@RequestParam("price") Integer price, Model model, HttpSession session, String roomtype) {
        if (session.getAttribute("user")==null) {
            session.setAttribute("searchId",hotelId);
            return "redirect:/signup" ;
        }

        model.addAttribute("username", user.getUsername());
        model.addAttribute("email", user.getEmail());
        model.addAttribute("hotel", orderService.findById(hotelId));
        model.addAttribute("price", price);
        model.addAttribute("photo2", null);
        model.addAttribute("photo3", null);
        model.addAttribute("photo4", null);
        if (roomtype.equals("2"))
            model.addAttribute("photo2", "../images/danrenjian.jpg");
        else if (roomtype.equals("3"))
            model.addAttribute("photo3", "../images/shuangrenjian.jpg");
        else if (roomtype.equals("4"))
            model.addAttribute("photo4", "../images/sirentaofang.jpg");
        String param = (String) SessionUtil.getSession().getAttribute("param");
        model.addAttribute("param1", param);
        roomPrice = price;

        String timeIn = (String) session.getAttribute("timeIn");
        String timeOut = (String) session.getAttribute("timeOut");
        model.addAttribute("timeIn", timeIn);
        model.addAttribute("timeOut", timeOut);

        model.addAttribute("DATE",timeIn+"至"+timeOut);

        return "details";
    }

    @PostMapping("/detailss")
    public String insertOrder(HotelOrder hOrder, HttpSession httpSession) throws ParseException {
        String timeOut = (String) httpSession.getAttribute("timeOut");
        User user = (User) httpSession.getAttribute("user");
        String timeIn = (String) httpSession.getAttribute("timeIn");

        Integer price = roomPrice * hOrder.getRooms();

        hOrder.setUsername(user.getUsername());
        hOrder.setEmail(user.getEmail());
        hOrder.setPrice(price);
        hOrder.setHotelId(Integer.toString(hotelId));
        hOrder.setUserId(this.user.getId());
        hOrder.setIfPay("未支付");
        String StartDate = timeIn;
        String LastDate = timeOut;
        hOrder.setStartData(StartDate);
        hOrder.setLastData(LastDate);
        orderService.insertOrder(hOrder);

        return "order";
    }

    @GetMapping("/order")
    public String findByOrderId(Model model,HttpSession httpSession,String orderId,String hotelId) {
        User user = (User) httpSession.getAttribute("user");


model.addAttribute("hotel",hotelMapper.selectByHotelId(hotelId)) ;
model.addAttribute("order",orderMapper.findByOrderId(Integer.valueOf(orderId)));

        return "order";
    }

    @GetMapping("/pay")
    public String pay(@RequestParam("orderId") Integer orderId, @RequestParam("price") Integer price) {
        return "redirect:/goAlipay?orderId=" + orderId + "&" + "price=" + price;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Integer deleteOrder(@RequestParam("orderId") Integer orderId) {
        return orderService.deleteOrder(orderId);
    }
}
